﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.API.Application.Command.MenuRoleCommand;
using RBAC.MaHaiBo.API.DTOS;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Service;
using System.Security.Cryptography;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 角色菜单关联 控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuRoleController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<MenuRoleController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public MenuRoleController(ILogger<MenuRoleController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }

        /// <summary>
        /// 树形菜单列表
        /// </summary>
        /// <returns>返回数据集合</returns>
        [HttpGet]
        public async Task<ApiResult<List<Tmeen>>> GetMenu([FromQuery] MenuRoleMenuShowCommand request)
        {
            ApiResult<List<Tmeen>> result = new ApiResult<List<Tmeen>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色菜单关联控制器】 树形菜单列表 仓储" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 树形角色列表
        /// </summary>
        /// <returns>返回数据集合</returns>
        [HttpGet]
        public async Task<ApiResult<List<Tmeen>>> GetRole()
        {
            ApiResult<List<Tmeen>> result = new ApiResult<List<Tmeen>>();
            try
            {
                result.Data = await mediator.Send(new MenuRoleRoleShowCommand());
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色菜单关联控制器】 树形角色列表 仓储" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加角色菜单关联
        /// </summary>
        /// <param name="Rid">角色</param>
        /// <param name="Mid">菜单编号集合</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMenuRole(MenuRoleCreateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色菜单关联服务层】 添加角色菜单关联 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 根据角色编号查询角色菜单关联中的菜单
        /// </summary> BackillMenuRole
        /// <param name="Rid">角色</param>
        /// <returns>返回根据角色编号查询角色菜单关联中的菜单的编号</returns>
        [HttpGet]
        public async Task<ApiResult<List<int>>> BackillMenuRole([FromQuery]BackillMenuRoleCommand request)
        {
            ApiResult<List<int>> result = new ApiResult<List<int>>();
            try
            {
                result.Data=await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色菜单关联服务层】 根据角色编号查询角色菜单关联中的菜单 出错" + ex.Message);
                throw;
            }
            return result;
        }

    }
}
